package at.socialconference.web.client.ui;

import at.socialconference.inter.model.Place;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MapPopUp extends PopupPanel {
	public MapPopUp(Place place){
		this.setGlassEnabled(true);
		
		this.setTitle(place.getName());
		setMap(place);
		
		this.center();
	}
	
	
	private void setMap(Place place) {
		if(place.getLatitude()!=null&&place.getLatitude()!=0.0&&place.getLongitude()!=null&&place.getLongitude()!=0.0){
			VerticalPanel vp = new VerticalPanel();
			double lat = place.getLatitude();
			double lon = place.getLongitude();

			LatLng location = LatLng.newInstance(lat,lon);
			
			final MapWidget map = new MapWidget(location, 2);
			map.setSize("500px", "500px");
			map.addControl(new LargeMapControl());
			map.setZoomLevel(17);
			final Marker marker = new Marker(location);
			map.addOverlay(marker);
			
			final InfoWindow infoWin = map.getInfoWindow();
			infoWin.open(map.getCenter(), new InfoWindowContent(place.getName()));
			
			vp.add(map);
			
			Button btn = new Button();
			btn.setText("Close");
			btn.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					MapPopUp.this.hide();
				}
			});
			vp.add(btn);
			this.add(vp);
		}else{
			this.hide();
		}
	}
}
